/**
 * ItemMgr.java
 * @author GorillaFunch
 * @created 2012.10.12
 */
package kr.ac.kongju.gedl.attach.service.impl;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import kr.ac.kongju.gedl.attach.dao.AttachFileDAO;
import kr.ac.kongju.gedl.attach.service.AttachFileService;
import kr.ac.kongju.gedl.attach.vo.AttachFileVO;
import kr.ac.kongju.gedl.common.util.ListResultVO;
import kr.ac.kongju.gedl.item.dao.ItemDAO;
import kr.ac.kongju.gedl.item.service.ItemService;
import kr.ac.kongju.gedl.item.vo.ItemFileVO;
import kr.ac.kongju.gedl.item.vo.ItemInfoVO;
import kr.ac.kongju.gedl.item.vo.ItemLevelVO;
import kr.ac.kongju.gedl.item.vo.ItemLinkVO;
import kr.ac.kongju.gedl.item.vo.ItemXmlVO;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import egovframework.rte.fdl.cmmn.AbstractServiceImpl;

@Service("attachFileService")
public class AttachFileServiceImpl  extends AbstractServiceImpl implements AttachFileService{
	// Log
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource(name="attachFileDAO")
    private AttachFileDAO attachFileDAO;

	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#registerAttachFile(kr.ac.kongju.gedl.attach.vo.AttachFileVO)
	 */
	@Override
	public AttachFileVO registerAttachFile(AttachFileVO attachFile){
		return attachFileDAO.registerAttachFile(attachFile);
	}
	
	@Override
	public List<AttachFileVO> registerAttachFileList(List<AttachFileVO> attachFileList){
		for(AttachFileVO attachFileVO : attachFileList) {
			attachFileVO = attachFileDAO.registerAttachFile(attachFileVO);
		}
		return attachFileList;
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#retrieveAttachFile(kr.ac.kongju.gedl.attach.vo.AttachFileVO)
	 */
	@Override
	public AttachFileVO retrieveAttachFile(AttachFileVO attachFile){
		return attachFileDAO.retrieveAttachFile(attachFile);
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#updateAttachFile(kr.ac.kongju.gedl.attach.vo.AttachFileVO)
	 */
	@Override
	public int updateAttachFile(AttachFileVO attachFile){
		return attachFileDAO.updateAttachFile(attachFile);
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#updateListAttachFile(kr.ac.kongju.gedl.attach.vo.AttachFileVO)
	 */
	@Override
	public int updateListAttachFile(AttachFileVO attachFile){
		return attachFileDAO.updateListAttachFile(attachFile);
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#deleteAttachFile(kr.ac.kongju.gedl.attach.vo.AttachFileVO)
	 */
	@Override
	public int deleteAttachFile(AttachFileVO attachFile){
		this.deleteAttachRealFile(attachFile);
		return attachFileDAO.deleteAttachFile(attachFile);
	}
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#deleteListAttachFile(com.ssc.common.service.SearchVO)
	 */
	@Override
	public int deleteListAttachFile(AttachFileVO searchCondition){
		List<AttachFileVO> list = attachFileDAO.retrieveListAttachFile(searchCondition);
		if(list != null && list.size() > 0){
			for(AttachFileVO attachFile:list){
				logger.info("[DEBUG] delete real file:{} path:{}", this.deleteAttachRealFile(attachFile), attachFile.getAttachFilePath());
			}
		}
		
		return attachFileDAO.deleteListAttachFile(searchCondition);
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#deleteListAttachFile(java.lang.String, java.lang.String)
	 */
	@Override
	public int deleteListAttachFile(String targetTable, String targetId){
		AttachFileVO searchCondition = new AttachFileVO();
		
		
		return this.deleteListAttachFile(searchCondition);
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#retrieveListCountAttachFile(com.ssc.common.service.SearchVO)
	 */
	@Override
	public int retrieveListCountAttachFile(AttachFileVO searchCondition) {
		return attachFileDAO.retrieveListCountAttachFile(searchCondition);	
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#retrieveListAttachFile(com.ssc.common.service.SearchVO)
	 */
	@Override
	public ListResultVO<AttachFileVO> retrieveListAttachFile(AttachFileVO searchCondition) {
		ListResultVO<AttachFileVO> listResult = new ListResultVO<AttachFileVO>();
		List<AttachFileVO> list = new ArrayList<AttachFileVO>();
		
		int count = attachFileDAO.retrieveListCountAttachFile(searchCondition);
		if(count > 0){
			list = attachFileDAO.retrieveListAttachFile(searchCondition);
			listResult = new ListResultVO<AttachFileVO>(count, list);
		}
		logger.info("count:{} list.size:{}", count, list.size());
			
		return listResult;	
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#selectListAttachFile(com.ssc.common.service.SearchVO)
	 */
	@Override
	public List<AttachFileVO> selectListAttachFile(AttachFileVO searchCondition) {
		
		List<AttachFileVO> list = attachFileDAO.retrieveListAttachFile(searchCondition);
			
		return list;	
	}
	
	/* (non-Javadoc)
	 * @see kr.ac.kongju.gedl.attach.service.impl.AttachFileService#deleteAttachRealFile(kr.ac.kongju.gedl.attach.vo.AttachFileVO)
	 */
	@Override
	public boolean deleteAttachRealFile(AttachFileVO attachFile){
		String path = attachFile.getAttachFilePath();
		boolean isDeleteFile = false;
		File file = new File(path);
		if(file.delete()){
			isDeleteFile = true;
		}
		return isDeleteFile;
	}	
}
